package presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.RectangleInsets;

import control.CControl;

public class PLiveview extends JPanel implements ComponentListener{
	private static final long serialVersionUID = 1L;
	private ChartPanel speedPanel, rpmPanel, speedGraph, rpmGraph;
	private JFreeChart speedDial, rpmDial;
	private PSmallGraphPanel graphs;
	private CControl cControl;

	public PLiveview(CControl cControl) {
		this.addComponentListener(this);
		this.setLayout(new GridBagLayout());
		this.cControl = cControl;
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;

		/** Webcam-image */
		PVideoPanel pVideoPanel = new PVideoPanel(cControl);
		this.cControl.addVideoObserver(pVideoPanel);
		pVideoPanel.setMaximumSize(new Dimension(800,600));
		c.weighty = 0;
		c.weightx = 0;
		c.fill = GridBagConstraints.NONE;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 2;
		this.add(pVideoPanel, c);
        
		/** Speedometer */
		PBatteryJuiceDial speed = new PBatteryJuiceDial();//PSpeedDial(cControl.getRegisterBoundaries("FSSpeed"));
		speedDial = new JFreeChart(speed);
		speedDial.setBackgroundPaint(Color.white);
		speedPanel = new ChartPanel(speedDial);
		speedPanel.setMinimumSize(new Dimension(192,192));
		speedPanel.setPreferredSize(new Dimension(192,192));
		speedPanel.setMaximumSize(new Dimension(192,192));

		
		cControl.addRegisterObserver("FSN_Speed", speed);
		
		c.weightx = 0.5;
		c.weighty = 0;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		//this.add(speedPanel, c);
		
		/** SoC */
		PBatteryJuiceDial soc = new PBatteryJuiceDial();
		JFreeChart socDial = new JFreeChart(soc);
		socDial.setBackgroundPaint(Color.white);
		ChartPanel socPanel = new ChartPanel(socDial);
//		socPanel.setMinimumSize(new Dimension(192,192));
		socPanel.setPreferredSize(new Dimension(192,192));
		socPanel.setMaximumSize(new Dimension(192,192));
		cControl.addRegisterObserver("MBN_BMS_SOCHV", soc);
		
		c.weightx = 0;
		c.weighty = 0;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 3;
		//this.add(socPanel, c);
		
		
		
		JPanel ptem = new JPanel();
		
		BoxLayout b = new BoxLayout(ptem, BoxLayout.Y_AXIS);
		
		ptem.setLayout(b);
		
		ptem.add(speedPanel);
		ptem.add(socPanel);
		
		this.add(ptem,c);
		
		/*placeholder panel*/
		c.weightx = 1;
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 2;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 3;
		this.add(new JPanel(),c);
		
		/** Safety status*/
		c.weightx = 0.5;
		c.weighty = 0.0;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 3;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 3;
		PSafetyStatus ss = new PSafetyStatus(cControl);
		
		ss.setMinimumSize(new Dimension(120,400));
		ss.setMaximumSize(new Dimension(120,400));
		this.add(ss, c);
		
		/** RPM-meter */
		//PRPMDial rpm = new PRPMDial(cControl.getRegisterBoundaries("RPM"));
		//rpmDial = new JFreeChart(rpm);
		//rpmDial.setBackgroundPaint(Color.white);
		//rpmPanel = new ChartPanel(rpmDial);
		//rpmPanel.setPreferredSize(new Dimension(0,192));
		//cControl.addRegisterObserver("RPM", rpm);
		//cControl.addRegisterObserver("Gear", rpm);
		//c.weightx = 0.5;
		//c.weighty = 0.0;
		//c.fill = GridBagConstraints.BOTH;
		//c.gridx = 2;
		//c.gridy = 0;
		//c.gridwidth = 1;
		//c.gridheight = 1;
		//this.add(rpmPanel, c);
		
		/** Graphs */
		
		speedGraph = new ChartPanel(PChartFactory.createFixedXYGraph("Sample", "Speed [km/h]",cControl.getRegisterBoundaries("FSSpeed"),20));
		speedGraph.setPreferredSize(new Dimension(120,120));
		//rpmGraph = new ChartPanel(PChartFactory.createFixedXYGraph("Sample", "RPM",cControl.getRegisterBoundaries("RPM"),2000));
		//rpmGraph.setPreferredSize(new Dimension(0,120));
		//graphs = new PSmallGraphPanel(speedGraph,rpmGraph);
		//cControl.addRegisterObserver("FSN_Speed", graphs);
		//cControl.addRegisterObserver("RPM", graphs);
		
		//c.weightx = 0.0;
		//c.weighty = 0.0;
		//c.fill = GridBagConstraints.BOTH;
		//c.gridx = 1;
		//c.gridy = 1;
		//c.gridwidth = 2;
		//c.gridheight = 1;
		//this.add(graphs, c);
		
		/** GPS-map */
		c.weightx = 0;
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		this.add(new PGPSPanel(cControl), c);
		
		/** Dashboard settings */
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 2;
		c.gridwidth = 2;
		c.gridheight = 1;
		//this.add(new PDashboardPanel(cControl), c);
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {}

	@Override
	public void componentMoved(ComponentEvent arg0) {}

	@Override
	public void componentResized(ComponentEvent arg0) {
		//int frameHeight = this.getHeight();
		int dialWidth = (this.getWidth() - 640)/2;
		int newDialHeight = (dialWidth > 240) ? 240 : dialWidth;
		int graphHeight = 480 - newDialHeight;
		
		speedPanel.setPreferredSize(new Dimension(0,newDialHeight));
		speedDial.setPadding(new RectangleInsets(
				0,(dialWidth-newDialHeight)/2,0,(dialWidth-newDialHeight)/2));
		//rpmPanel.setPreferredSize(new Dimension(0,newDialHeight));
		//rpmDial.setPadding(new RectangleInsets(
				//0,(dialWidth-newDialHeight)/2,0,(dialWidth-newDialHeight)/2));
		
		if (graphHeight > dialWidth) {
			speedGraph.setPreferredSize(new Dimension(0,(480-newDialHeight)/2));
			//graphs.setLayout(new GridLayout(2,1,0,0));
		} else {
			speedGraph.setPreferredSize(new Dimension(0,(480-newDialHeight)));
			//graphs.setLayout(new GridLayout(1,2,0,0));
		}
		this.updateUI();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {}
}




